<?php
class SessionDB {
    /**
     *加载模块
    */
    public static function load(){ }
}

/**
 * @return PdoDb Description
 */
function ssn_getdb(){
    return App::getDB();
}

function ssn_query_one($sql){
    return ssn_getdb()->queryOne($sql);
}

function ssn_exec($sql){
    return ssn_getdb()->exec($sql);
}

function ssn_open($path,$name){
    return true;
}
function ssn_close(){
    return true;
}
function ssn_read($key){
    $cmdStr="select *
from sys_session
where s_id='{$key}' and exp_time > now()";
    $data=ssn_query_one($cmdStr);
    if(!empty($data)){
        return $data["content"];
    }
    return "";
}
function ssn_write($key,$value){
    $now=time();
    $expTime=date("Y-m-d H:i:s",$now+180*60);
    $db=  ssn_getdb();
    
    $sqlParam=array(
        "k"=>$key,
        "c"=>$value,
        "e"=>$expTime
    );
    $sqlUpdate="update sys_session set content=:c,exp_time=:e
        where s_id=:k";
    $n=$db->exec($sqlUpdate, $sqlParam);
    if($n>0){
        return $n;
    }
    $cmdStr="insert into sys_session(s_id,content,exp_time)
values(:k,:c,:e)";
    $n=  ssn_getdb()->exec($cmdStr, $sqlParam);
    return $n;
}
function ssn_destroy($key){
    $cmdStr="delete from sys_session where s_id='{$key}'";
    $n=ssn_exec($cmdStr);
    return $n;
}
function ssn_gc($expTime){
    $cmdStr="delete from sys_session where exp_time< now()";
    $n=ssn_exec($cmdStr);
    return $n;
}
ini_set("session.save_handler","user");
session_set_save_handler(
    "ssn_open",
    "ssn_close",
    "ssn_read",
    "ssn_write",
    "ssn_destroy",
    "ssn_gc"
);